package Demo03.Demo08;

import java.io.*;
import java.util.Scanner;

public class Demo_Demo08 {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入A文件夹的路径");
        String next1 = sc.next();
        System.out.println("请输入B文件夹的路径");
        String next2 = sc.next();
        File file1 = new File(next1);
        File file2 = new File(next2);
        copy(file1,file2);
        sc.close();
    }

    private static void copy(File file1, File file2) throws IOException {
        if (file1.isFile()){
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file1));
            file2.getParentFile().mkdir();
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file2));
            byte[] bytes = new byte[1024];
            int temp;
            while ((temp = bis.read(bytes))!=-1){
                bos.write(bytes,0,temp);
                bos.flush();
            }
            System.out.println("拷贝完成");
            bos.close();
            bis.close();
        }else {
            File[] files = file1.listFiles();
            if (files !=null){
                for (int i = 0; i < files.length; i++) {
                    copy(files[i],new File(file2.getPath()+"/"+files[i].getName()));
                }
            }
        }
    }
}
